package cn.school.service.impl;

import cn.hutool.core.collection.CollectionUtil;

import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import cn.school.dto.PaperDetailDto;
import cn.school.dto.UpdatePaperDetailDto;
import cn.school.mapper.PaperDetailMapper;
import cn.school.mapper.TeacherMapper;
import cn.school.pojo.PaperDetail;
import cn.school.pojo.Teacher;
import cn.school.service.PaperDetailService;
import cn.school.vo.PaperDetailVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【paper_detail(试卷详情表)】的数据库操作Service实现
* @createDate 2025-06-09 18:41:20
*/
@Service
public class PaperDetailServiceImpl extends ServiceImpl<PaperDetailMapper, PaperDetail>
    implements PaperDetailService {
    @Resource
    private PaperDetailMapper paperDetailMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Override
    public String addDetail(PaperDetailDto paperDetailDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object userId = jwt.getPayload("id");
        QueryWrapper<Teacher> teacherQueryWrapper=new QueryWrapper();
        teacherQueryWrapper.eq("user_id",userId);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        if(teacher==null){
            return "您没有权限对试卷进行操作";
        }   else {
            QueryWrapper<PaperDetail> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("paper_id",paperDetailDto.getPaperId());
            List<PaperDetail> paperDetails = paperDetailMapper.selectList(queryWrapper);
            if(CollectionUtil.isEmpty(paperDetails)){
                PaperDetail paperDetail = new PaperDetail();
                paperDetail.setId(IdUtil.getSnowflake(1,1).nextId());
                String paperId = paperDetailDto.getPaperId();
                paperDetail.setPaperId(Long.valueOf(paperId));
                paperDetail.setQuestionId(1);
                paperDetail.setQuestionUrl(paperDetailDto.getQuestionUrl());
                paperDetail.setQuestionDetail(paperDetailDto.getQuestionDetail());
                paperDetail.setQuestionValue(paperDetailDto.getQuestionValue());
                paperDetail.setQuestionAnswer(paperDetailDto.getQuestionAnswer());
                paperDetail.setIsDel(0);
                paperDetailMapper.insert(paperDetail);
                return "添加成功";
            }else {
                List<Integer> list=new ArrayList<>();
                for (PaperDetail paperDetail : paperDetails) {
                    list.add(paperDetail.getQuestionId());
                }
                Integer max = CollectionUtil.max(list);
                max=max+1;
                PaperDetail paperDetail = new PaperDetail();
                paperDetail.setId(IdUtil.getSnowflake(1,1).nextId());
                String paperId = paperDetailDto.getPaperId();
                paperDetail.setPaperId(Long.valueOf(paperId));
                paperDetail.setQuestionId(max);
                paperDetail.setQuestionUrl(paperDetailDto.getQuestionUrl());
                paperDetail.setQuestionDetail(paperDetailDto.getQuestionDetail());
                paperDetail.setQuestionValue(paperDetailDto.getQuestionValue());
                paperDetail.setQuestionAnswer(paperDetailDto.getQuestionAnswer());
                paperDetail.setIsDel(0);
                paperDetailMapper.insert(paperDetail);
                return "添加成功";
            }
        }
    }

    @Override
    public List<PaperDetailVo> findPaperDetail(String paperId) {
        QueryWrapper<PaperDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("paper_id",paperId);
        List<PaperDetail> paperDetails = paperDetailMapper.selectList(queryWrapper);
        List<PaperDetailVo> paperDetailVos = new ArrayList<>();
        for (PaperDetail paperDetail : paperDetails) {
            PaperDetailVo paperDetailVo = new PaperDetailVo();
            paperDetailVo.setId(String.valueOf(paperDetail.getId()));
            paperDetailVo.setQuestionId(paperDetail.getQuestionId());
            paperDetailVo.setPaperId(String.valueOf(paperDetail.getPaperId()));
            paperDetailVo.setQuestionUrl(paperDetail.getQuestionUrl());
            paperDetailVo.setQuestionDetail(paperDetail.getQuestionDetail());
            paperDetailVo.setQuestionValue(paperDetail.getQuestionValue());
            paperDetailVo.setQuestionAnswer(paperDetail.getQuestionAnswer());
            paperDetailVo.setIsDel(paperDetail.getIsDel());
            paperDetailVos.add(paperDetailVo);
        }
        return paperDetailVos;
    }

    @Override
    public String updatePaperDetail(UpdatePaperDetailDto updatePaperDetailDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object userId = jwt.getPayload("id");
        QueryWrapper<Teacher> teacherQueryWrapper=new QueryWrapper();
        teacherQueryWrapper.eq("user_id",userId);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        if(teacher==null){
            return "您没有权限对试卷进行操作";
        }   else {
            QueryWrapper<PaperDetail> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("paper_id",updatePaperDetailDto.getPaperId());
            queryWrapper.eq("question_id",updatePaperDetailDto.getQuestionId());
            PaperDetail paperDetail = paperDetailMapper.selectOne(queryWrapper);
            paperDetail.setQuestionUrl(updatePaperDetailDto.getQuestionUrl());
            paperDetail.setQuestionDetail(updatePaperDetailDto.getQuestionDetail());
            paperDetail.setQuestionValue(updatePaperDetailDto.getQuestionValue());
            paperDetail.setQuestionAnswer(updatePaperDetailDto.getQuestionAnswer());
            int id = paperDetailMapper.updateById(paperDetail);
            if(id>0){
                return "修改成功";
            }   else {
                return "修改失败";
            }
        }
    }
}




